/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Functions.java
 *
 * Created on Jun 4, 2011, 12:21:44 AM
 */
package eu.anorien.dmo.gui;

import eu.anorien.dmo.DistributedMatlabOptimization;
import eu.anorien.dmo.Function;
import eu.anorien.dmo.Parameter;
import eu.anorien.dmo.Server;
import eu.anorien.dmo.gui.optimization.Optimization;
import java.awt.GridBagLayout;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class Functions extends javax.swing.JPanel {

    private DistributedMatlabOptimization dmo;
    private ArrayList<Server> servers;
    private ArrayList<Function> functions;
    private Parameter currentParameter = null;
    private int nameGen = 0;
    private boolean updating = false;
    private DefaultTableModel runCustomValuesTableModel;
    private Function runCustomValuesCurrentFunction;

    /** Creates new form Functions */
    public Functions(DistributedMatlabOptimization dmo, ArrayList<Function> functions, ArrayList<Server> servers) {
        this.functions = functions;
        this.servers = servers;
        this.dmo = dmo;
        initComponents();
        paramTypeDialog.pack();
        runCustomValuesDialog.add(runCustomValuesPanel);
        paramTypeDialog.setLocationRelativeTo(null);
        funcList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                Functions.this.update();
            }
        });
        paramList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                Functions.this.update();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        doubleParamPanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        doubleMinTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        doubleMaxTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        doubleStepTextField = new javax.swing.JTextField();
        jButton8 = new javax.swing.JButton();
        booleanParamPanel = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        paramTypeDialog = new javax.swing.JDialog();
        paramTypeComboBox = new javax.swing.JComboBox();
        jButton7 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        newParamNameTextField = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        noParamPanel = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        runCustomValuesPanel = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        customValuesTable = new javax.swing.JTable();
        setValuePanel = new javax.swing.JPanel();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        doubleValuePanel = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        doubleValueTextField = new javax.swing.JTextField();
        booleanValuePanel = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        booleanValueComboBox = new javax.swing.JComboBox();
        runCustomValuesDialog = new javax.swing.JDialog();
        integerParamPanel = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        integerMinTextField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        integerMaxTextField = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        integerStepTextField = new javax.swing.JTextField();
        jButton13 = new javax.swing.JButton();
        integerValuePanel = new javax.swing.JPanel();
        jLabel17 = new javax.swing.JLabel();
        integerValueTextField = new javax.swing.JTextField();
        optimizationTypeDialog = new javax.swing.JDialog();
        jButton14 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        optimizationTypeList = new javax.swing.JList();
        jButton11 = new javax.swing.JButton();
        functionDetailsPanel = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        funcNameTextField = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        paramListScrollPane = new javax.swing.JScrollPane();
        paramList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        noFunctionLabel = new javax.swing.JLabel();
        noParamLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        funcList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        paramDetailsContainerPanel = new javax.swing.JPanel();
        functionDetailsContainerPanel = new javax.swing.JPanel();

        jLabel3.setText("Min value:");

        doubleMinTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doubleMinTextFieldActionPerformed(evt);
            }
        });

        jLabel4.setText("Max value:");

        doubleMaxTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doubleMaxTextFieldActionPerformed(evt);
            }
        });

        jLabel5.setText("Num steps:");

        doubleStepTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doubleStepTextFieldActionPerformed(evt);
            }
        });

        jButton8.setText("Apply changes");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout doubleParamPanelLayout = new javax.swing.GroupLayout(doubleParamPanel);
        doubleParamPanel.setLayout(doubleParamPanelLayout);
        doubleParamPanelLayout.setHorizontalGroup(
            doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(doubleParamPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(doubleParamPanelLayout.createSequentialGroup()
                        .addGroup(doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(doubleStepTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                            .addComponent(doubleMaxTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                            .addComponent(doubleMinTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)))
                    .addComponent(jButton8, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        doubleParamPanelLayout.setVerticalGroup(
            doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(doubleParamPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(doubleMinTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(doubleMaxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(doubleParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(doubleStepTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton8)
                .addContainerGap(67, Short.MAX_VALUE))
        );

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("(no configuration required)");

        javax.swing.GroupLayout booleanParamPanelLayout = new javax.swing.GroupLayout(booleanParamPanel);
        booleanParamPanel.setLayout(booleanParamPanelLayout);
        booleanParamPanelLayout.setHorizontalGroup(
            booleanParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        booleanParamPanelLayout.setVerticalGroup(
            booleanParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
        );

        paramTypeDialog.setTitle("New Parameter");
        paramTypeDialog.setAlwaysOnTop(true);

        paramTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Integer", "Double", "Boolean" }));

        jButton7.setText("ok");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel12.setText("Name:");

        jLabel13.setText("Type:");

        javax.swing.GroupLayout paramTypeDialogLayout = new javax.swing.GroupLayout(paramTypeDialog.getContentPane());
        paramTypeDialog.getContentPane().setLayout(paramTypeDialogLayout);
        paramTypeDialogLayout.setHorizontalGroup(
            paramTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paramTypeDialogLayout.createSequentialGroup()
                .addGroup(paramTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(paramTypeDialogLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(paramTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13))
                        .addGap(18, 18, 18)
                        .addGroup(paramTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(paramTypeComboBox, 0, 245, Short.MAX_VALUE)
                            .addComponent(newParamNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, paramTypeDialogLayout.createSequentialGroup()
                        .addContainerGap(299, Short.MAX_VALUE)
                        .addComponent(jButton7)))
                .addContainerGap())
        );
        paramTypeDialogLayout.setVerticalGroup(
            paramTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paramTypeDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(paramTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(newParamNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(paramTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(paramTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton7)
                .addContainerGap())
        );

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("(select a parameter)");

        javax.swing.GroupLayout noParamPanelLayout = new javax.swing.GroupLayout(noParamPanel);
        noParamPanel.setLayout(noParamPanelLayout);
        noParamPanelLayout.setHorizontalGroup(
            noParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
        );
        noParamPanelLayout.setVerticalGroup(
            noParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
        );

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Run function with custom parameter values");

        customValuesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Param", "Value"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        customValuesTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                customValuesTableMouseClicked(evt);
            }
        });
        customValuesTable.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                customValuesTablePropertyChange(evt);
            }
        });
        customValuesTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                customValuesTableKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(customValuesTable);

        setValuePanel.setMinimumSize(new java.awt.Dimension(100, 10));
        setValuePanel.setLayout(new javax.swing.BoxLayout(setValuePanel, javax.swing.BoxLayout.LINE_AXIS));

        jButton9.setText("set");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("run");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout runCustomValuesPanelLayout = new javax.swing.GroupLayout(runCustomValuesPanel);
        runCustomValuesPanel.setLayout(runCustomValuesPanelLayout);
        runCustomValuesPanelLayout.setHorizontalGroup(
            runCustomValuesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, runCustomValuesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(runCustomValuesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                    .addComponent(jButton10)
                    .addGroup(runCustomValuesPanelLayout.createSequentialGroup()
                        .addComponent(setValuePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton9)))
                .addContainerGap())
        );
        runCustomValuesPanelLayout.setVerticalGroup(
            runCustomValuesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(runCustomValuesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(runCustomValuesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(runCustomValuesPanelLayout.createSequentialGroup()
                        .addComponent(jButton9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton10))
                    .addComponent(setValuePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jLabel10.setText("Value:");

        doubleValueTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doubleValueTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout doubleValuePanelLayout = new javax.swing.GroupLayout(doubleValuePanel);
        doubleValuePanel.setLayout(doubleValuePanelLayout);
        doubleValuePanelLayout.setHorizontalGroup(
            doubleValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(doubleValuePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addGap(18, 18, 18)
                .addComponent(doubleValueTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
        doubleValuePanelLayout.setVerticalGroup(
            doubleValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(doubleValuePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(doubleValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(doubleValueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jLabel11.setText("Value:");

        booleanValueComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));
        booleanValueComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                booleanValueComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout booleanValuePanelLayout = new javax.swing.GroupLayout(booleanValuePanel);
        booleanValuePanel.setLayout(booleanValuePanelLayout);
        booleanValuePanelLayout.setHorizontalGroup(
            booleanValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(booleanValuePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addGap(18, 18, 18)
                .addComponent(booleanValueComboBox, 0, 225, Short.MAX_VALUE)
                .addContainerGap())
        );
        booleanValuePanelLayout.setVerticalGroup(
            booleanValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(booleanValuePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(booleanValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(booleanValueComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        runCustomValuesDialog.setTitle("Run function with custom parameters");
        runCustomValuesDialog.getContentPane().setLayout(new javax.swing.BoxLayout(runCustomValuesDialog.getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jLabel14.setText("Min value:");

        integerMinTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                integerMinTextFieldActionPerformed(evt);
            }
        });

        jLabel15.setText("Max value:");

        integerMaxTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                integerMaxTextFieldActionPerformed(evt);
            }
        });

        jLabel16.setText("Num steps:");

        integerStepTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                integerStepTextFieldActionPerformed(evt);
            }
        });

        jButton13.setText("Apply changes");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout integerParamPanelLayout = new javax.swing.GroupLayout(integerParamPanel);
        integerParamPanel.setLayout(integerParamPanelLayout);
        integerParamPanelLayout.setHorizontalGroup(
            integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(integerParamPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(integerParamPanelLayout.createSequentialGroup()
                        .addGroup(integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel14)
                            .addComponent(jLabel15)
                            .addComponent(jLabel16))
                        .addGap(18, 18, 18)
                        .addGroup(integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(integerStepTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                            .addComponent(integerMaxTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                            .addComponent(integerMinTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)))
                    .addComponent(jButton13, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        integerParamPanelLayout.setVerticalGroup(
            integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(integerParamPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(integerMinTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(integerMaxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(integerParamPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(integerStepTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton13)
                .addContainerGap(54, Short.MAX_VALUE))
        );

        jLabel17.setText("Value:");

        integerValueTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                integerValueTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout integerValuePanelLayout = new javax.swing.GroupLayout(integerValuePanel);
        integerValuePanel.setLayout(integerValuePanelLayout);
        integerValuePanelLayout.setHorizontalGroup(
            integerValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(integerValuePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel17)
                .addGap(18, 18, 18)
                .addComponent(integerValueTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
        integerValuePanelLayout.setVerticalGroup(
            integerValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(integerValuePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(integerValuePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(integerValueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jButton14.setText("optimize");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        optimizationTypeList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "MonteCarloOptimization", "HillClimbingOptimization", "SimulatedAnnealingOptimization", "GeneticOptimization" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        optimizationTypeList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane4.setViewportView(optimizationTypeList);

        javax.swing.GroupLayout optimizationTypeDialogLayout = new javax.swing.GroupLayout(optimizationTypeDialog.getContentPane());
        optimizationTypeDialog.getContentPane().setLayout(optimizationTypeDialogLayout);
        optimizationTypeDialogLayout.setHorizontalGroup(
            optimizationTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optimizationTypeDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(optimizationTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        optimizationTypeDialogLayout.setVerticalGroup(
            optimizationTypeDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optimizationTypeDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton14)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton11.setText("run with custom values");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jLabel6.setText("Name:");

        funcNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                funcNameTextFieldActionPerformed(evt);
            }
        });
        funcNameTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                funcNameTextFieldPropertyChange(evt);
            }
        });

        paramList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                paramListValueChanged(evt);
            }
        });
        paramListScrollPane.setViewportView(paramList);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Parameters:");

        jButton3.setText("new");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("delete");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("up");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("down");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(paramListScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paramListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton5)
                    .addComponent(jButton6))
                .addContainerGap())
        );

        jButton15.setText("set");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout functionDetailsPanelLayout = new javax.swing.GroupLayout(functionDetailsPanel);
        functionDetailsPanel.setLayout(functionDetailsPanelLayout);
        functionDetailsPanelLayout.setHorizontalGroup(
            functionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(functionDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(funcNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton15)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        functionDetailsPanelLayout.setVerticalGroup(
            functionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(functionDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(functionDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(funcNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        noFunctionLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        noFunctionLabel.setText("<select a function>");
        noFunctionLabel.setOpaque(true);

        noParamLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        noParamLabel.setText("<select a parameter>");
        noParamLabel.setOpaque(true);

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        funcList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                funcListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(funcList);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Functions");

        jButton1.setText("new");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton12.setText("optimize");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                    .addComponent(jButton12)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton12)
                .addContainerGap())
        );

        paramDetailsContainerPanel.setMinimumSize(new java.awt.Dimension(100, 100));
        paramDetailsContainerPanel.setLayout(new java.awt.GridBagLayout());

        functionDetailsContainerPanel.setMinimumSize(new java.awt.Dimension(100, 100));
        functionDetailsContainerPanel.setLayout(new java.awt.GridBagLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(functionDetailsContainerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paramDetailsContainerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(paramDetailsContainerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
            .addComponent(functionDetailsContainerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void update() {
        if (updating) {
            return;
        }
        try {
            updating = true;
            Function currentFunctionSelection = (Function) funcList.getSelectedValue();
            Parameter currentParameterSelection = (Parameter) paramList.getSelectedValue();

            funcNameTextField.setText("");
            functionDetailsContainerPanel.removeAll();
            functionDetailsContainerPanel.setLayout(new GridBagLayout());
            functionDetailsContainerPanel.add(noFunctionLabel);
            paramDetailsContainerPanel.removeAll();
            paramDetailsContainerPanel.setLayout(new GridBagLayout());
            paramDetailsContainerPanel.add(noParamLabel);
            paramList.setModel(new DefaultComboBoxModel());

            funcList.setModel(new DefaultComboBoxModel(functions.toArray()));
            if (currentFunctionSelection != null) {
                funcList.setSelectedValue(currentFunctionSelection, true);
            }

            if (funcList.getSelectedValue() != null && !((Function) funcList.getSelectedValue()).isLocked()) {
                Function selectedFunc = ((Function) funcList.getSelectedValue());
                funcNameTextField.setText(selectedFunc.getName());

                paramList.setModel(new DefaultComboBoxModel(selectedFunc.getParameters().toArray()));
                if (currentParameterSelection != null && selectedFunc.getParameters().contains(currentParameterSelection)) {
                    paramList.setSelectedValue(currentParameterSelection, true);
                }

                functionDetailsContainerPanel.removeAll();
                functionDetailsContainerPanel.setLayout(new BoxLayout(functionDetailsContainerPanel, BoxLayout.LINE_AXIS));
                functionDetailsContainerPanel.add(functionDetailsPanel);
                functionDetailsContainerPanel.validate();
                functionDetailsContainerPanel.repaint();

                if (paramList.getSelectedValue() != null) {
                    Parameter selectedParam = ((Parameter) paramList.getSelectedValue());

                    paramDetailsContainerPanel.removeAll();
                    paramDetailsContainerPanel.setLayout(new BoxLayout(paramDetailsContainerPanel, BoxLayout.LINE_AXIS));

                    if (selectedParam instanceof Parameter.DoubleParameter) {
                        Parameter.DoubleParameter doubleParameter = (Parameter.DoubleParameter) selectedParam;
                        doubleMaxTextField.setText("" + doubleParameter.getMax());
                        doubleMinTextField.setText("" + doubleParameter.getMin());
                        doubleStepTextField.setText("" + doubleParameter.getNSteps());
                        paramDetailsContainerPanel.add(doubleParamPanel);
                    } else if (selectedParam instanceof Parameter.IntegerParameter) {
                        Parameter.IntegerParameter integerParameter = (Parameter.IntegerParameter) selectedParam;
                        integerMaxTextField.setText("" + integerParameter.getMax());
                        integerMinTextField.setText("" + integerParameter.getMin());
                        integerStepTextField.setText("" + integerParameter.getNSteps());
                        paramDetailsContainerPanel.add(integerParamPanel);
                    } else if (selectedParam instanceof Parameter.BooleanParameter) {
                        Parameter.BooleanParameter booleanParameter = (Parameter.BooleanParameter) selectedParam;
                        paramDetailsContainerPanel.add(booleanParamPanel);
                    } else {
                        throw new RuntimeException("BUG");
                    }
                    paramDetailsContainerPanel.repaint();
                }
            }
            paramDetailsContainerPanel.validate();
            
            this.validateTree();
            this.repaint();
        } finally {
            updating = false;
        }
    }

    private void funcNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_funcNameTextFieldActionPerformed
        Function selectedFunction = null;
        if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
            selectedFunction.setName(funcNameTextField.getText());
            update();
        }
    }//GEN-LAST:event_funcNameTextFieldActionPerformed

    private void funcNameTextFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_funcNameTextFieldPropertyChange
    }//GEN-LAST:event_funcNameTextFieldPropertyChange

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Function newF = new Function("func" + nameGen++, new ArrayList<Parameter>());
        functions.add(newF);
        update();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void doubleMinTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doubleMinTextFieldActionPerformed
        readDoubleParamSettings();
    }

    private void readDoubleParamSettings() throws NumberFormatException {
        Parameter selectedParameter = null;
        if ((selectedParameter = (Parameter) paramList.getSelectedValue()) != null) {
            ((Parameter.DoubleParameter) selectedParameter).setMin(Double.parseDouble(doubleMinTextField.getText()));
            ((Parameter.DoubleParameter) selectedParameter).setMax(Double.parseDouble(doubleMaxTextField.getText()));
            ((Parameter.DoubleParameter) selectedParameter).setNSteps(Integer.parseInt(doubleStepTextField.getText()));
        }
        update();
    }

    private void readIntegerParamSettings() throws NumberFormatException {
        Parameter selectedParameter = null;
        if ((selectedParameter = (Parameter) paramList.getSelectedValue()) != null) {
            ((Parameter.IntegerParameter) selectedParameter).setMin(Integer.parseInt(integerMinTextField.getText()));
            ((Parameter.IntegerParameter) selectedParameter).setMax(Integer.parseInt(integerMaxTextField.getText()));
            ((Parameter.IntegerParameter) selectedParameter).setNSteps(Integer.parseInt(integerStepTextField.getText()));
        }
        update();
    }//GEN-LAST:event_doubleMinTextFieldActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Function selectedFunction = null;
        if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
            functions.remove(selectedFunction);
            update();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        moveParam(-1);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        moveParam(1);
    }

    private void moveParam(int move) {
        Parameter selectedParameter = null;
        if ((selectedParameter = (Parameter) paramList.getSelectedValue()) != null) {
            Function selectedFunction = null;
            if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
                int oldIdx = selectedFunction.getParameters().indexOf(selectedParameter);
                if (oldIdx == 0 || oldIdx == selectedFunction.getParameters().size() - 1) {
                    return;
                }
                selectedFunction.getParameters().remove(selectedParameter);
                selectedFunction.getParameters().add(oldIdx + move, selectedParameter);
                update();
            }
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        Parameter selectedParameter = null;
        if ((selectedParameter = (Parameter) paramList.getSelectedValue()) != null) {
            Function selectedFunction = null;
            if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
                selectedFunction.getParameters().remove(selectedParameter);
                update();
            }
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.setEnabled(false);
        Function selectedFunction = null;
        if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
            paramTypeDialog.setVisible(true);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        this.setEnabled(true);
        paramTypeDialog.setVisible(false);
        Function selectedFunction = null;
        if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
            if (paramTypeComboBox.getSelectedItem().equals("Double")) {
                Parameter param = new Parameter.DoubleParameter(newParamNameTextField.getText(), 0, 10, 1);
                selectedFunction.getParameters().add(param);
            } else if (paramTypeComboBox.getSelectedItem().equals("Integer")) {
                Parameter param = new Parameter.IntegerParameter(newParamNameTextField.getText(), 0, 10, 1);
                selectedFunction.getParameters().add(param);
            } else if (paramTypeComboBox.getSelectedItem().equals("Boolean")) {
                Parameter param = new Parameter.BooleanParameter();
                selectedFunction.getParameters().add(param);
            } else {
                throw new RuntimeException("BUG");
            }
            update();
        }
    }//GEN-LAST:event_jButton7ActionPerformed

    private void doubleMaxTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doubleMaxTextFieldActionPerformed
        readDoubleParamSettings();
    }//GEN-LAST:event_doubleMaxTextFieldActionPerformed

    private void doubleStepTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doubleStepTextFieldActionPerformed
        readDoubleParamSettings();
    }//GEN-LAST:event_doubleStepTextFieldActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        readDoubleParamSettings();
    }//GEN-LAST:event_jButton8ActionPerformed

    private void funcListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_funcListValueChanged
        update();
    }//GEN-LAST:event_funcListValueChanged

    private void paramListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_paramListValueChanged
        update();
    }//GEN-LAST:event_paramListValueChanged

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed

        Function selectedFunction = null;
        if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
            runCustomValuesCurrentFunction = selectedFunction;
            updateRunCustomValueTable();
            runCustomValuesDialog.pack();
            runCustomValuesDialog.setVisible(true);
        }
    }//GEN-LAST:event_jButton11ActionPerformed

    private void updateRunCustomValueTable() {
        runCustomValuesTableModel = new DefaultTableModel(new String[]{"Parameter", "Value"}, 0);
        customValuesTable.setModel(runCustomValuesTableModel);

        for (Parameter parameter : runCustomValuesCurrentFunction.getParameters()) {
            String name;
            Object value;
            if (parameter instanceof Parameter.DoubleParameter) {
                name = parameter.getName();
                value = 0.0;
            } else if (parameter instanceof Parameter.IntegerParameter) {
                name = parameter.getName();
                value = 0;
            } else if (parameter instanceof Parameter.BooleanParameter) {
                name = parameter.getName();
                value = false;
            } else {
                throw new RuntimeException("Bug");
            }
            runCustomValuesTableModel.addRow(new Object[]{name, value});
        }
    }

    private void customValuesTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_customValuesTablePropertyChange
    }//GEN-LAST:event_customValuesTablePropertyChange

    private void doubleValueTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doubleValueTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_doubleValueTextFieldActionPerformed

    private void booleanValueComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_booleanValueComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_booleanValueComboBoxActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        int selectedRow = customValuesTable.getSelectedRow();
        if (selectedRow != -1) {
            Parameter parameter = runCustomValuesCurrentFunction.getParameters().get(selectedRow);

            if (parameter instanceof Parameter.DoubleParameter) {
                runCustomValuesTableModel.setValueAt(Double.parseDouble(doubleValueTextField.getText()), selectedRow, 1);
            } else if (parameter instanceof Parameter.IntegerParameter) {
                runCustomValuesTableModel.setValueAt(Integer.parseInt(integerValueTextField.getText()), selectedRow, 1);
            } else if (parameter instanceof Parameter.BooleanParameter) {
                runCustomValuesTableModel.setValueAt(booleanValueComboBox.getSelectedIndex() == 0, selectedRow, 1);
            } else {
                throw new RuntimeException("Bug");
            }
        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        String parameters = "";
        if (runCustomValuesTableModel.getRowCount() > 0) {
            parameters = "" + runCustomValuesTableModel.getValueAt(0, 1);
        }
        for (int i = 1; i < runCustomValuesTableModel.getRowCount(); i++) {
            parameters += ", " + runCustomValuesTableModel.getValueAt(i, 1);
        }

        Server server = servers.get(0);
        try {
            server.getProxy().runFunction(server.getPassword(), runCustomValuesCurrentFunction.getName(), parameters);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jButton10ActionPerformed

    private void customValuesTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_customValuesTableKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_customValuesTableKeyPressed

    private void customValuesTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_customValuesTableMouseClicked
        int selectedRow = customValuesTable.getSelectedRow();
        if (selectedRow != -1) {
            Object value = runCustomValuesTableModel.getValueAt(selectedRow, 1);
            Parameter parameter = runCustomValuesCurrentFunction.getParameters().get(selectedRow);

            if (parameter instanceof Parameter.DoubleParameter) {
                setValuePanel.removeAll();
                setValuePanel.add(doubleValuePanel);
                doubleValueTextField.setText("" + value);
            } else if (parameter instanceof Parameter.IntegerParameter) {
                setValuePanel.removeAll();
                setValuePanel.add(integerValuePanel);
                integerValueTextField.setText("" + value);
            } else if (parameter instanceof Parameter.BooleanParameter) {
                setValuePanel.removeAll();
                setValuePanel.add(booleanValuePanel);
                booleanValueComboBox.setSelectedIndex((Boolean) value ? 0 : 1);
            } else {
                throw new RuntimeException("Bug");
            }
            setValuePanel.validate();
        }
    }//GEN-LAST:event_customValuesTableMouseClicked

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        optimizationTypeDialog.pack();
        optimizationTypeDialog.setLocationRelativeTo(null);
        optimizationTypeDialog.setVisible(true);
    }//GEN-LAST:event_jButton12ActionPerformed

    private void integerMinTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_integerMinTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_integerMinTextFieldActionPerformed

    private void integerMaxTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_integerMaxTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_integerMaxTextFieldActionPerformed

    private void integerStepTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_integerStepTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_integerStepTextFieldActionPerformed

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
        readIntegerParamSettings();
    }//GEN-LAST:event_jButton13ActionPerformed

    private void integerValueTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_integerValueTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_integerValueTextFieldActionPerformed

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
        optimizationTypeDialog.setVisible(false);
        Function selectedFunction = null;
        if ((selectedFunction = (Function) funcList.getSelectedValue()) != null) {
            try {
                String optimizationType = "" + optimizationTypeList.getSelectedValue();
                Class optType = Class.forName("eu.anorien.dmo.gui.optimization." + optimizationType);
                Constructor c = optType.getConstructor(new Class[]{Function.class, ArrayList.class});
                Object o = c.newInstance(selectedFunction, servers);
                dmo.startOptimization((Optimization) o);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_jButton14ActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        update();
    }//GEN-LAST:event_formComponentShown

    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton15ActionPerformed
        funcNameTextFieldActionPerformed(null);
    }//GEN-LAST:event_jButton15ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel booleanParamPanel;
    private javax.swing.JComboBox booleanValueComboBox;
    private javax.swing.JPanel booleanValuePanel;
    private javax.swing.JTable customValuesTable;
    private javax.swing.JTextField doubleMaxTextField;
    private javax.swing.JTextField doubleMinTextField;
    private javax.swing.JPanel doubleParamPanel;
    private javax.swing.JTextField doubleStepTextField;
    private javax.swing.JPanel doubleValuePanel;
    private javax.swing.JTextField doubleValueTextField;
    private javax.swing.JList funcList;
    private javax.swing.JTextField funcNameTextField;
    private javax.swing.JPanel functionDetailsContainerPanel;
    private javax.swing.JPanel functionDetailsPanel;
    private javax.swing.JTextField integerMaxTextField;
    private javax.swing.JTextField integerMinTextField;
    private javax.swing.JPanel integerParamPanel;
    private javax.swing.JTextField integerStepTextField;
    private javax.swing.JPanel integerValuePanel;
    private javax.swing.JTextField integerValueTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextField newParamNameTextField;
    private javax.swing.JLabel noFunctionLabel;
    private javax.swing.JLabel noParamLabel;
    private javax.swing.JPanel noParamPanel;
    private javax.swing.JDialog optimizationTypeDialog;
    private javax.swing.JList optimizationTypeList;
    private javax.swing.JPanel paramDetailsContainerPanel;
    private javax.swing.JList paramList;
    private javax.swing.JScrollPane paramListScrollPane;
    private javax.swing.JComboBox paramTypeComboBox;
    private javax.swing.JDialog paramTypeDialog;
    private javax.swing.JDialog runCustomValuesDialog;
    private javax.swing.JPanel runCustomValuesPanel;
    private javax.swing.JPanel setValuePanel;
    // End of variables declaration//GEN-END:variables
}
